package com.yimo.controller;

import com.xiaoju.automarket.knife.util.JsonUtil;
import com.xiaoju.automarket.knife.util.StringUtil;
import com.yimo.activity.common.module.ApiProductDTO;
import com.yimo.common.util.HttpClient;
import com.yimo.imgtransfer.Product;
import com.yimo.imgtransfer.QiniuService;
import com.yimo.imgtransfer.SpecValue;
import com.yimo.product.dto.ImgUDto;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * create by jsm on 2018/11/6
 */
@RestController
@RequestMapping("/imgtrans")
public class ImgTransferController {
    @Resource
    private HttpClient httpClient;
    @Resource
    private QiniuService qiniuService;

    @GetMapping("/doImage")
    public void tans() throws IOException {
        while (true) {
            String dogetImage = httpClient.doGet("http://www.yimobuy.com/productapi/getNeedTransImg");
            if (StringUtil.isEmpty(dogetImage)) {
                return;
            }
            ImgUDto img = JsonUtil.fromJson(dogetImage, ImgUDto.class);
            System.out.println("get image:" + JsonUtil.toJson(img));
            BufferedImage imageBuffer = ImageIO.read(httpClient.doGetStream(img.getUrl()));
            String qiuNiuUrl = qiniuService.uploadImage(imageBuffer);
            img.setUrl(qiuNiuUrl);
            System.out.println("update image:" + JsonUtil.toJson(img));
            httpClient.postJson(JsonUtil.toJson(img), "http://www.yimobuy.com/productapi/updateImg");
        }
    }

    @GetMapping("/doProduct")
    public void tans1() throws IOException {
        while (true) {
            String dogetProduct = httpClient.doGet("http://www.yimobuy.com/productapi/getNeedTransProduct");
            if (StringUtil.isEmpty(dogetProduct)) {
                return;
            }
            Product product = JsonUtil.fromJson(dogetProduct, Product.class);
            System.out.println("get product:" + JsonUtil.toJson(product));

            String detailHtml = product.getDetailHtml();
            String pattern = "https://cbu01.alicdn.com/img/ibank/.*?jpg";
            String imgextraPattern = "https://img.alicdn.com/imgextra/.*?.jpg";
            Pattern r1 = Pattern.compile(pattern);
            Matcher m1 = r1.matcher(detailHtml);
            Pattern r2 = Pattern.compile(imgextraPattern);
            Matcher m2 = r2.matcher(detailHtml);
            List <String> oldurls = new ArrayList <>();
            while (m1.find()) {
                oldurls.add(m1.group());
            }
            while (m2.find()) {
                oldurls.add(m2.group());
            }

            for (int i = 0; i < oldurls.size(); i++) {
                String oldUrl = oldurls.get(i);
                BufferedImage imageBuffer = ImageIO.read(httpClient.doGetStream(oldUrl));
                if (imageBuffer == null) {
                    continue;
                }
                detailHtml = detailHtml.replace(oldUrl, qiniuService.uploadImage(imageBuffer));
            }


            product.setDetailHtml(detailHtml);
            System.out.println("update image:" + JsonUtil.toJson(product));
            httpClient.postJson(JsonUtil.toJson(product), "http://www.yimobuy.com/productapi/updateProduct");
        }
    }

    @GetMapping("doSpecValue")

    public void doSpecValue() throws IOException {
        while (true){
            String dogetSpecValue = httpClient.doGet("http://www.yimobuy.com/productapi/getNeedTransSpecValue");
            if (StringUtil.isEmpty(dogetSpecValue)) {
                return;
            }
            SpecValue specValue= JsonUtil.fromJson(dogetSpecValue, SpecValue.class);
            System.out.println("get product:" + JsonUtil.toJson(specValue));
            BufferedImage imageBuffer = ImageIO.read(httpClient.doGetStream(specValue.getImg()));
            if(imageBuffer==null){
                continue;
            }
            String qiniuImage = qiniuService.uploadImage(imageBuffer);
            specValue.setImg(qiniuImage);
            httpClient.postJson(JsonUtil.toJson(specValue), "http://www.yimobuy.com/productapi/updateSpecValue");
        }

    }
}
